/*
 * @Author: wuzhouhai wzh200x@126.com
 * @Date: 2024-07-11 18:58:39
 * @LastEditors: wuzhouhai wzh200x@126.com
 * @LastEditTime: 2024-08-09 10:07:22
 * @FilePath: \dwys_web\assets\script\GameContrl.ts
 * @Description: 
 */
import { _decorator, Component, Node } from 'cc';
import { CCComp } from '../../extensions/oops-plugin-framework/assets/module/common/CCComp';
import { GameEvent } from './common/event/GameEvent';
import { oops } from '../../extensions/oops-plugin-framework/assets/core/Oops';
import { UIID } from './common/config/GameUIConfig';
import * as proto from "./phoenix/protoc/ts/proto_app.js";
const { ccclass, property } = _decorator;

@ccclass('GameContrl')
export class GameContrl extends CCComp {

    private selectPmt: proto.Iroom_pmt_info | undefined = undefined;
    private selectRoom: proto.Iroom_info | undefined = undefined;

    reset(): void {

    }
    
    protected onEnable(): void {
        oops.message.on(GameEvent.GameStartRun, this.onHandler, this);
        oops.message.on(GameEvent.LoginAccountServerComplete, this.onHandler, this);
        oops.message.on(GameEvent.SelectPmt, this.onSelectPmt, this);
        oops.message.on(GameEvent.SelectRoom, this.onSelectRoom, this);
    }

    protected onDestroy(): void {
        oops.message.off(GameEvent.GameStartRun, this.onHandler, this);
        oops.message.off(GameEvent.LoginAccountServerComplete, this.onHandler, this);
        oops.message.off(GameEvent.SelectPmt, this.onSelectPmt, this);
        oops.message.off(GameEvent.SelectRoom, this.onSelectRoom, this);
    }

    private onHandler(event: string, ...args: any) {
        switch (event) {
            case GameEvent.LoginAccountServerComplete:
                this.onLoginAccountServerComplete();
                break;
            case GameEvent.GameStartRun:
                this.onGameStartRun();
                break;
            default:
                break;
        }
    }

    onGameStartRun() {
        oops.gui.open(UIID.LoginUI);
    }

    onLoginAccountServerComplete() {
        oops.gui.remove(UIID.LoginUI);
        oops.message.dispatchEvent(GameEvent.StartLoginGameServer);
        oops.message.dispatchEvent(GameEvent.AddGameMsgInfo, "登录游戏服务器");
    }

    onSelectPmt(event:string, pmt:proto.Iroom_pmt_info){
        console.log("onSelectPmt", pmt);
        this.selectPmt = pmt;
    }

    onSelectRoom(event:string, room:proto.Iroom_info){
        console.log("onSelectRoom", room);
        oops.message.dispatchEvent(GameEvent.AddGameMsgInfo, "当前选中 [" + room.name + "]");
        this.selectRoom = room;
    }

    onClickPmtUpLevel(e:TouchEvent, arg:any){
        if(this.selectPmt){
            oops.message.dispatchEvent(GameEvent.UpRoomPmtLevel, this.selectPmt.roomId, this.selectPmt.pmtId);
        }    
    }

    onClickRoomUpLevel(e:TouchEvent, arg:any){
        if(this.selectRoom && this.selectRoom.active == 1){
            oops.message.dispatchEvent(GameEvent.UpRoomCyLevel, this.selectRoom.roomId);
        }    
    }

    onClickRoomActive(e:TouchEvent, arg:any){
        if(this.selectRoom){
            if (this.selectRoom.active == 1) {
                oops.message.dispatchEvent(GameEvent.AddGameMsgInfo, "[" + this.selectRoom.name + "] 已激活");
            } else {
                oops.message.dispatchEvent(GameEvent.ActiveRoom, this.selectRoom.roomId);
            }
        }    
    }

    update(deltaTime: number) {
        
    }
}


